#ifndef __MUTEX_H_
#define __MUTEX_H_

#include<iostream>
#include<pthread.h>
#include<unistd.h>
#include<string>
namespace MutexModule
{
    class Mutex
    {
    public:
        Mutex()
        {
            pthread_mutex_init(&_mutex,nullptr);
        }

        void Lock()
        {
            int n = pthread_mutex_lock(&_mutex);
            if(n!=0)
            {
                std::cout<<"lock error"<<std::endl;
                exit(1);
            }

        }
        void Unlock()
        {
            int n = pthread_mutex_unlock(&_mutex);
            if(n!=0)
            {
                std::cout<<"unlock error"<<std::endl;
                exit(1);
            }
        }

        ~Mutex()
        {
            pthread_mutex_destroy(&_mutex);
        }
    private:
        pthread_mutex_t _mutex;
    };

    class MutexGuard
    {
    public:
        MutexGuard(Mutex *mutex)
        :_mutex(mutex)
        {
            _mutex->Lock();
        }
        ~MutexGuard()
        {
            _mutex->Unlock();
        }
    private:
        Mutex *_mutex;
    };
}



#endif